import { defineStore } from "pinia";
import { useDictBaseStore } from "./dict-base";
import { ref } from "vue";

export const useDictStore = defineStore("dict", () => {
  const dicts = ref<any>([]);
  const dictBaseStore = useDictBaseStore();

  async function getDicts() {
    if (dicts.value.length > 0) dicts.value;
    dicts.value = await dictBaseStore.getDicts();
  }

  function label(groupCode: string, value: string) {
    return dicts.value.find((item: any) => item.groupCode === groupCode && item.value === value)?.label;
  }

  function value(groupCode: string, code: string) {
    return dicts.value.find((item: any) => item.groupCode === groupCode && item.code === code)?.value;
  }

  return {
    getDictGroupList: dictBaseStore.getDictGroupList,
    saveDictGroup: dictBaseStore.saveDictGroup,
    deleteDictGroup: dictBaseStore.deleteDictGroup,
    saveDict: dictBaseStore.saveDict,
    deleteDict: dictBaseStore.deleteDict,
    getDicts,
    label,
    value,
    dicts,

  };
});
